package com.lottery.im.module.service.presenter;

import android.app.Activity;
import android.util.Log;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.observer.ShowTipObserver2;
import com.lottery.im.api.result.ApiResult;
import com.lottery.im.api.result.entity.CanReceiveData0;
import com.lottery.im.api.result.entity.CanReceiveData1;
import com.lottery.im.api.result.entity.DoSign;
import com.lottery.im.api.result.entity.SignData;
import com.lottery.im.base.BaseFragmentActivity;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.SigninView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class SigninPresenter extends BaseFragmentPresenter<SigninView> {
  public SigninPresenter(Activity activity,
      SigninView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();

  public void loadUserSignData(String month){
    ApiClient.getApiRetrofitInstance()
        .getSignData(userId,token,month)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.<ApiResult<SignData>>bindToLifecycle())
        .subscribe(new ShowTipObserver<SignData>(activity) {
          @Override public void onSuccess(SignData returnMapBean) {
            iView.loadUserSignDataSuccess(returnMapBean);
          }
        });
    }

    //己领取积分信息
    public void loadCanReceiveData0(String type){
      ApiClient.getApiRetrofitInstance()
          .isCanReceive0(userId,token,type)
          .throttleFirst(2, TimeUnit.SECONDS)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(provider.bindToLifecycle())
          .subscribe(new ShowTipObserver2<CanReceiveData0.ReturnMapBean>(activity) {
            @Override public void onSuccess(CanReceiveData0.ReturnMapBean returnMapBean,String returnCode) {
              iView.loadCanReceiveData0Success(returnMapBean,returnCode);
            }
          });
    }

    //己领连续签到符号分信息
    public void loadCanReceiveData1(String type){
      ApiClient.getApiRetrofitInstance()
          .isCanReceive1(userId,token,type)
          .throttleFirst(2,TimeUnit.SECONDS)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(provider.bindToLifecycle())
          .subscribe(new ShowTipObserver2<CanReceiveData1.ReturnMapBean>(activity) {
            @Override public void onSuccess(CanReceiveData1.ReturnMapBean returnMapBean,String returnCode) {
              iView.loadCanReciveData1Success(returnMapBean,returnCode);
            }
          });
    }

    //领取签到
    public void onReceiveSignClick(String type){
      ApiClient.getApiRetrofitInstance()
          .ReceiveSignReword0(userId,token,type)
          .throttleFirst(2,TimeUnit.SECONDS)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(provider.bindToLifecycle())
          .subscribe(new ShowTipObserver2<CanReceiveData0.ReturnMapBean>(activity) {
            @Override
            public void onSuccess(CanReceiveData0.ReturnMapBean returnMapBean, String returnCode) {
              //iView.loadCanReceiveData0Success(returnMapBean,returnCode);
              loadCanReceiveData0("0");
            }
          });
    }

  public void onReceiveSignClick1(String type){
    ApiClient.getApiRetrofitInstance()
        .ReceiveSignReword1(userId,token,type)
        .throttleFirst(2,TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipObserver2<CanReceiveData1.ReturnMapBean>(activity) {
          @Override
          public void onSuccess(CanReceiveData1.ReturnMapBean returnMapBean, String returnCode) {
            //iView.loadCanReciveData1Success(returnMapBean,returnCode);
            loadCanReceiveData1("1");
          }
        });
  }

  //用户签到操作
    public void onSignClick(){
      ApiClient.getApiRetrofitInstance()
          .userSign(userId,token)
          .throttleFirst(2,TimeUnit.SECONDS)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(provider.<ApiResult<DoSign>>bindToLifecycle())
          .subscribe(new ShowTipObserver<DoSign>(activity) {
            @Override public void onSuccess(DoSign doSign) {
              iView.userSignDataSuccess();
            }
          });
    }
}
